package cn.com.wxd.biz.website;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.wxd.dao.website.CatalogDao;
import cn.com.wxd.entity.website.ArticleInfo;
import cn.com.wxd.entity.website.CatalogInfo;
import cn.com.wxd.common.CharUtil;
import cn.com.wxd.common.data.KVMap;
import cn.com.wxd.common.data.KeyValue;
import cn.com.wxd.util.website.WebsiteInfo;

/**
 * Title:CatalogServiceImpl.java
 * Description:cn.com.wxd.biz.catalog
 * <p>
 * Company: Amumu管理平台
 * Copyright: Copyright (c) 2015
 * All right reserved.
 * Created time: 2015年10月22日 下午5:32:35
 *
 * @author WangXuDong
 * @version 1.0
 */
public class CatalogServiceImpl implements CatalogService {
    private CatalogDao catalogDao;
    private static final Logger log = LoggerFactory.getLogger(CatalogServiceImpl.class);

    @Override
    public List<CatalogInfo> getAllCatalogFormDb(Map<String, String> param) {
        return catalogDao.selectAllCatalog(param);
    }

    @Override
    public List<CatalogInfo> getAllCatalog(Map<String, String> param) {
        List<CatalogInfo> l = null;
        if (param == null || param.size() == 0) {
            if (WebsiteInfo.isCache()) {
                l = WebsiteCache.getCatalog();
            }
        } else {
            l = getAllCatalogFormDb(param);
        }
        return l;
    }

    @Override
    public KVMap<String, CatalogInfo> getAllCatalogKVMap(
            Map<String, String> param) {
        KVMap<String, CatalogInfo> kv = new KVMap<String, CatalogInfo>();

        List<CatalogInfo> l = null;
        if (param == null || param.size() == 0) {
            if (WebsiteInfo.isCache()) {
                l = WebsiteCache.getCatalog();
            }
        } else {
            l = catalogDao.selectAllCatalog(param);
        }
        for (int i = 0; i < l.size(); i++) {
            kv.put(l.get(i).getCataCode(), l.get(i));
        }
        return kv;
    }

    @Override
    public CatalogInfo getCatalogById(String id) {
        List<CatalogInfo> catali = WebsiteCache.getCatalog();
        if (WebsiteInfo.isCache()) {
            if (catali != null) {
                for (int i = 0; i < catali.size(); i++) {
                    if (catali.get(i).getId() == CharUtil.stringParsInt(id)) {
                        return catali.get(i);
                    }
                }
            } else {
                return catalogDao.getCatalogById(CharUtil.stringParsInt(id));
            }
        }
        return catalogDao.getCatalogById(CharUtil.stringParsInt(id));
    }

    @Override
    public List<CatalogInfo> getRootCatalog() {
        List<CatalogInfo> catali = WebsiteCache.getCatalog();
        List<CatalogInfo> recatali = new ArrayList<>();
        if (WebsiteInfo.isCache()) {
            if (catali != null) {
                for (int i = 0; i < catali.size(); i++) {
                    if (catali.get(i).getParentId() == null || catali.get(i).getParentId().equals("")) {
                        recatali.add(catali.get(i));
                    }
                }
                if (recatali.size() > 0) {
                    return recatali;
                }
            }
        }
        return catalogDao.selectRootCatalog();
    }

    @Override
    public List<CatalogInfo> getSubCatalog(String id) {
        List<CatalogInfo> catali = WebsiteCache.getCatalog();
        List<CatalogInfo> recatali = new ArrayList<CatalogInfo>();
        if (WebsiteInfo.isCache()) {
            if (catali != null) {
                for (int i = 0; i < catali.size(); i++) {
                    if (catali.get(i).getParentId() != null && catali.get(i).getParentId()
                            .equals(CharUtil.stringParsInt(id))) {
                        recatali.add(catali.get(i));
                    }
                }
                if (recatali.size() > 0) {
                    return recatali;
                }
            }
        }
        return catalogDao.selectSubCatalogById(CharUtil.stringParsInt(id));
    }

    @Override
    public boolean addCatalog(CatalogInfo cli) {
        
        if (cli.getOrderId() == null || cli.getOrderId() <= 0) {
            cli.setOrderId(catalogDao.selectMaxOrderId() + 1);
        }
        boolean b = catalogDao.insertCatalog(cli);
        if (b && WebsiteInfo.isCache()) {
            WebsiteCache.refreshCatalogCache();  //刷新栏目缓存
        }
        return b;
    }

    @Override
    public boolean deleteCatalog(String id) {
        boolean b = catalogDao.deleteCatalog(CharUtil.stringParsInt(id));
        if (b && WebsiteInfo.isCache()) {
            WebsiteCache.refreshCatalogCache();  //刷新栏目缓存
        }
        return b;
    }

    @Override
    public boolean isExistArt(String caId) {

        long in = 0;
        if (WebsiteInfo.isCache()) {
            List<ArticleInfo> artli = WebsiteCache.getWebsiteCache().get(CharUtil.stringParsInt(caId));
            if (artli == null) {
                in = 0;
            } else {
                in = artli.size();
            }
        }
        if (in <= 0) {
            in = catalogDao.selectArtCountByCaid(CharUtil.stringParsInt(caId));
        }
        if (in > 0) {
            return true;
        } else if (in == -1) {
            try {
                throw new Exception("查询文章记录数错误！");
            } catch (Exception e) {
                log.error("", e);
            }
        }
        return false;
    }

    @Override
    public boolean isExistCata(String caId) {
        int in = 0;
        if (WebsiteInfo.isCache()) {
            List<CatalogInfo> list = WebsiteCache.getCatalog();
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getParentId() == null) {
                        continue;
                    }
                    if (list.get(i).getParentId().intValue() == CharUtil.stringParsInt(caId)) {
                        in++;
                    }
                }
            }
        }
        if (in <= 0) {
            in = catalogDao.selectSubCatalogCount(CharUtil.stringParsInt(caId));
        }
        if (in > 0) {
            return true;
        } else if (in == -1) {
            try {
                throw new Exception("查询子栏目记录数错误！");
            } catch (Exception e) {
                log.error("", e);
            }
        }
        return false;
    }

    @Override
    public boolean isExistCataCode(String cataCode) {
        if (WebsiteInfo.isCache()) {
            List<CatalogInfo> list = WebsiteCache.getCatalog();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getCataCode().equals(cataCode)) {
                    return true;
                }
            }
            return false;
        }
        if (catalogDao.selectCatalogBycode(cataCode) == null) {
            return false;
        }
        return true;
    }

    @Override
    public List<KeyValue<Integer, String>> getCatalogNav(String cataid) {
        List<KeyValue<Integer, String>> listkeyvalue = new ArrayList<>();
        int cataid2 = CharUtil.stringParsInt(cataid);
        while (cataid2 > 0) {
            KeyValue<Integer, String> keyvalue = new KeyValue<>();

            CatalogInfo ci = null;
            if (WebsiteInfo.isCache()) {
                List<CatalogInfo> catali = WebsiteCache.getCatalog();
                if (catali != null) {
                    for (int i = 0; i < catali.size(); i++) {
                        if (catali.get(i).getId() == cataid2) {
                            ci = catali.get(i);
                            break;
                        }
                    }
                    if (ci == null) {
                        ci = catalogDao.getCatalogById(cataid2);
                    }
                }
            } else {
                ci = catalogDao.getCatalogById(cataid2);
            }
            if (ci == null) {
                break;
            }
            keyvalue.setKey(ci.getId()); //设置键为ID 值为名称
            keyvalue.setValue(ci.getName());
            if (ci.getParentId() != null) {
                cataid2 = ci.getParentId();
            } else {
                cataid2 = 0;
            }
            listkeyvalue.add(keyvalue);
        }

        return listkeyvalue;
    }

    @Override
    /**
     * 栏目的排序
     */
    public boolean rankCatalog(String newOrderId, String oldOrderId) {
        int newOrdId = CharUtil.stringParsInt(newOrderId);
        int oldOrdId = CharUtil.stringParsInt(oldOrderId);
        if (newOrdId == -1 || oldOrdId == -1) {
            return false;
        } else if (newOrdId == oldOrdId) {
            return true;
        } else {
            if (catalogDao.updateCatalogOrder(newOrdId, oldOrdId)) {  //开始数据库中替换排序号
                if (WebsiteInfo.isCache()) {
                    WebsiteCache.refreshCatalogCache();
                }
                return true;
            }
            return false;
        }

    }

    public CatalogDao getCatalogDao() {
        return catalogDao;
    }

    public void setCatalogDao(CatalogDao catalogDao) {
        this.catalogDao = catalogDao;
    }
}
